package com.sigmatrix.h5.web.controller.front;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sigmatrix.h5.auth.ILoginUser;
import com.sigmatrix.h5.config.LoginUser;
import com.sigmatrix.h5.config.TableUtils;
import com.sigmatrix.h5.entity.v2.CodeFleeCheckRecord;
import com.sigmatrix.h5.entity.v2.Product;
import com.sigmatrix.h5.exception.MessageException;
import com.sigmatrix.h5.service.common.ServiceHelper;
import com.sigmatrix.h5.service.db.CodeFleeCheckRecordDBService;
import com.sigmatrix.h5.service.db.ProductDBService;
import com.sigmatrix.h5.service.inspect.CurrentQrCode;
import com.sigmatrix.h5.service.inspect.ICodeInspect;
import com.sigmatrix.h5.web.BaseController;
import com.sigmatrix.h5.web.args.CodeFleeParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 
 * @author wei.wang
 *
 */

@Api(tags="串货")
@RestController
@RequestMapping("front/flee")
public class FleeController extends BaseController{
	
	@Autowired
	private ServiceHelper serviceHelper;
	
	@Autowired
	private ProductDBService productDBService;
	
	@Autowired
	private CodeFleeCheckRecordDBService codeFleeCheckRecordDBService;
	
//	待测未使用。
//	@ApiOperation("窜货(根据码流转)")
//	@PostMapping("/process")
//	public List<CodeFleeCheckRecord> flee(@LoginUser ILoginUser user,@RequestBody @ApiParam(name="串货检查入参",value="串货检查入参",required=true)  CodeFleeParam param){
//		CurrentQrCode qrCode = serviceHelper.selectService(user.getEseCode(),ICodeInspect.class).getQrCode(param.getQrCode(), param.getCodeType(),user.getEseCode());
//		Product product = productDBService.selectById(qrCode.getProduct().getId(),TableUtils.tablePrefixBy(user.getEseCode()));
//		List<CodeFleeCheckRecord> recoreds = serviceHelper.selectService(user.getEseCode(),ICodeInspect.class).fleeByCirculation(user.getEseCode(), qrCode, product);
//		return recoreds;
//	}
	
	
	@ApiOperation("显示串货记录")
	@PostMapping("/record/show")
	public List<CodeFleeCheckRecord> fleeRecordShow(@LoginUser ILoginUser user,@RequestBody @ApiParam(name="串货检查入参",value="串货检查入参",required=true) CodeFleeParam param){
		if(param.getFleeRecodIds()==null){
			throw new MessageException("参数不正确");
		}
		String[] split = param.getFleeRecodIds().split(",");
		if(split==null||split.length==0){
			throw new MessageException("参数不正确");
		}
		Set<Integer> idSet = new HashSet<>();

		try {
			for (String str : split) {
				idSet.add(Integer.valueOf(str));
			}
		} catch (NumberFormatException e) {
			
			throw new MessageException("参数不正确");
		}
		
		Integer[] ids = idSet.toArray(new Integer[0]);

		return codeFleeCheckRecordDBService.findByIds(ids, user.getEseCode());
	}
	
	
	
}
